<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>FileHelpers Library - Marcos Meli</TITLE>
<LINK href="/favicon.ico" rel="SHORTCUT ICON"><LINK 
href="global.css" type=text/css 
rel=stylesheet>
<link type="text/css" rel="stylesheet" href="SyntaxHighlighter.css"></link>

<meta NAME="Description"        content="An easy to use .NET library to read/write data from fixed length or delimited files or streams. It has support to import/export data from different data sources." />
<meta name="Author"             content="Marcos Meli" />
<meta NAME="keywords" Content="filehelpers,CSV,Marcos_Meli,C#,free,opensource,Devoo.net,Delimited Files,Import/Export">
<meta NAME="Copyright" CONTENT="Copyright 2005-07 to Marcos Meli - www.Devoo.net. All Rights Reserved.">

</HEAD>
<BODY   text=black vLink=purple aLink=red link=blue 
bgColor=white leftMargin=0 topMargin=0>

		{$HEADER$}


<table width="100%" cellpadding="5" cellspacing="5">
<tr><td><div id=contentdiv>  <h2>Custom Converter Example </h2>
        <blockquote>
          <p>If you like to parse a file with a field that has different parsing rules you can define a CustomConverter, for example if you have this source file:</p>
          <blockquote>

<textarea name="code" class="c#:nogutter:nocontrols">
Argentina|Meli, Marcos|25
Argentina|Meli, Luciano|25
Argentina|Perez, Juan|25
Chile|Salas, Marcelo|25
</textarea>

          </blockquote>
          <p>The solution is to define a class for the Name part:</p>
          <blockquote>
<textarea name="code" class="c#">
public class NameField
 {
     public string FirstName;
     public string LastName;

     public override string ToString()
     {
         return LastName + &quot;, &quot; + FirstName;
     }
}
</textarea>
          </blockquote>
          <p>Later define your record class:</p>
          <blockquote>
<textarea name="code" class="c#">
[DelimitedRecord(&quot;|&quot;)]
public class CustomConvClass
{
   public string Country;
   
   [FieldConverter(typeof(NameConverter))]
   public NameField Names;

   public int Age;
}</pre>
</textarea>

          </blockquote>
          <p>The big step is to define the converter you must inherit from the <a href="FileHelpers.ConverterBase.html">ConverterBase</a> </p>
          <blockquote>
<textarea name="code" class="c#">
public class NameConverter: ConverterBase
{
   public override object StringToField(string from)
   {
       string[] splited = from.Split(',');

       NameField res = new NameField();
       res.LastName = splited[0].Trim();
       res.FirstName = splited[1].Trim();

       return res;
   }
}
</textarea>

          </blockquote>
          <p>Done !! you can actually parse the file with.</p>
          <blockquote>
<textarea name="code" class="c#">
FileHelperEngine  engine = new FileHelperEngine(typeof(CustomConvClass));

CustomConvClass[] res = (CustomConvClass[]) engine.ReadFile("NameTest.txt");

// You can access your custom field like this

  res[0].Country
  res[0].Names.LastName
  res[i].Names.FirstName
  res[i].Age
</textarea>
  
          </blockquote>
        </blockquote>

		{$FOOTER$}
</tr>
</table>

</BODY></HTML>
